﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SlimDX;

namespace EikonLeap
{
    public class Input
    {
        public Input(System.Windows.Forms.Form form)
        {
            //Leap.Controller controller = new Leap.Controller();
            m_wndForm = form;
            m_leapDevice = new LeapDimension();
            m_leapDevice.Initialize();
            m_leapDevice.SwipeBegin += new LeapDimension.SwipeEventHandler(m_leapDevice_SwipeBegin);
            m_leapDevice.SwipeUpdate += m_leapDevice_SwipeUpdate;
            m_leapDevice.SwipeEnd += new LeapDimension.SwipeEventHandler(m_leapDevice_SwipeEnd);
            m_leapDevice.ScreenTap += new LeapDimension.ScreenTapEventHandler(m_leapDevice_ScreenTap);
            m_leapDevice.HandMove += new LeapDimension.HandMoveEventHandler(m_leapDevice_HandMove);
            SlimDX.DirectInput.DirectInput input = new SlimDX.DirectInput.DirectInput();
            var devices = input.GetDevices();
            var mouseInstance = devices.Select(x => x.Type == SlimDX.DirectInput.DeviceType.Mouse ? x : null).FirstOrDefault();
            Mouse = new SlimDX.DirectInput.Mouse(input);
            Mouse.Properties.AxisMode = SlimDX.DirectInput.DeviceAxisMode.Relative;
            Mouse.Acquire();
        }

        void m_leapDevice_ScreenTap(ScreenTapEventArgs arg)
        {
            if (m_zoom == 1.0f)
            {
                m_zoom *= 2.0f;
            }
            else
            {
                m_zoom = 1.0f;
            }
        }

        void m_leapDevice_HandMove(HandMoveEventArgs arg)
        {
            if (arg.Hand.Fingers.Empty)
            {
                return;
            }

            Leap.Finger fing = arg.Hand.Fingers[0];

            float x = fing.TipPosition.x;
            float y = fing.TipPosition.y;

            leapPos.X = x;
            leapPos.Y = y;

            m_zoom = ((arg.Hand.SphereRadius - 60.0f) / 40.0f) + 1.0f;
            if (m_zoom < 1.0f) m_zoom = 1.0f;
            if (m_zoom > 2.0f) m_zoom = 2.0f;

        }

        Vector2 leapPos = new Vector2();

        public float m_zoom = 1.0f;
        float m_floatSweepSpeed = 0.0f;
        float swipeSpeed = 0.0f;
        void m_leapDevice_SwipeBegin(SwipeEventArgs arg)
        {
            swipeSpeed = arg.Gesture.Speed;
        }

        void m_leapDevice_SwipeUpdate(SwipeEventArgs arg)
        {
            swipeSpeed *= 0.8f;
        }

        void m_leapDevice_SwipeEnd(SwipeEventArgs arg)
        {
            m_floatSweepSpeed = arg.Gesture.Direction.x * swipeSpeed * 0.00001f;
            //swipeSpeed = 0.0f;
        }

        ~Input()
        {
            m_leapDevice.Terminate();
            Mouse.Unacquire();
        }

        public float SweepSpeed { get { return m_floatSweepSpeed; } set { m_floatSweepSpeed = value; } }
        public SlimDX.DirectInput.Mouse Mouse { get; set; }
        public Vector2 CursorPosition
        {
            get
            {
                bool mouseCursor = false;
                if (mouseCursor)
                {
                    var p = m_wndForm.PointToClient(System.Windows.Forms.Cursor.Position);
                    return new Vector2(p.X, p.Y);
                }
                else
                {
                    float x = (leapPos.X + 150.0f) / 300.0f * m_wndForm.ClientSize.Width;
                    float y = m_wndForm.ClientSize.Height - ((leapPos.Y - 100.0f) / 200.0f * m_wndForm.ClientSize.Height);
                    System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)x, (int)y);
                    var p = m_wndForm.PointToClient(new System.Drawing.Point((int)x, (int)y));
                    return new Vector2(p.X, p.Y);
                }
            }
        }
        private System.Windows.Forms.Form m_wndForm;
        private LeapDimension m_leapDevice;
    }
}
